<?php
/**
 * Created by PhpStorm.
 * User: 13790
 * Date: 2018/3/22
 * Time: 10:50
 */

namespace app\admin\controller;


use Think\Controller;
use think\Exception;
use Think\Model;

class Role extends Base
{
  public function lst(){
      $data=model('Role')->select();

      return view('',['data'=>$data]);
  }
  public function add(){
      if (request()->isPost()){
          try{
              $res=Model('Role')->addData();
              if ($res!==true){
                  Exception($res);
              }
          }catch (\Exception $e){
              $this->error($e->getMessage());
          }
          $this->success('添加成功');
      }
      $auth_data = db('Auth')->order('auth_ids')->select();

      return view('add',['auth_data'=>$auth_data]);

  }

    public function del($id){
        $m = db('Role');
        $num_rows = $m->where("id={$id}")->delete();
        if($num_rows>0){
            $url = U('Index/index');
            $this->success('删除成功！',"{$url}");
//            $this->redirect('Index/fun', '', 1, '删除中');
        }else{
            $this->error('删除失败！');
        }
    }
    public function edit(){

        if (request()->isPost()) {
            try {
                $res = model('Role')->editData();
                //当更新没有成功但是没有抛异常，咱把更新失败这几个字抛出
                if ($res !== true) {
                    Exception($res);
                }
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('更新成功', 'lst');
        }
        $id = input('param.id');
        $data = model('Role')
            ->alias('a')
            ->field('a.*, group_concat(b.auth_id) a_ids')
            ->join('role_auth b','a.id = b.role_id','LEFT')
            ->find($id);

        //查询所有的角色信息
        $auth_data = db('Auth')->select();
        return view('',['data'=>$data,'auth_data'=>$auth_data]);
    }

}